PROJECT	= profiling
RM			= rm -f 
#RM			= del
TAGS		= ctags
DEVICE	= atmega168
#F_CPU	= 12000000
F_CPU	= 14745600

# Fuses: http://www.engbedded.com/fusecalc/
# Mega8
#FUSE_L	= 0xFF
##FUSE_L	= 0xE1
#FUSE_H	= 0xD9
#
# Mega168
FUSE_L	= 0xFF
FUSE_H	= 0xDF
FUSE_E	= 0x01

#AVRDUDE	= avrdude -p m168 -c stk500v2 -y -P COM4 # edit this line for your programmer, this settings fits Windows system.
#AVRDUDE	= avrdude -p m8 -c stk500v2 -y -P COM4 # edit this line for your programmer, this settings fits Windows system.
#AVRDUDE	= avrdude -p m8 -c stk500v2 -y -Y 94 -P COM4 # Use -Y # option to reser the counter of programming cyccles.
#AVRDUDE	= avrdude -p m168 -c stk500v2 -y -P /dev/ttyUSB0 # This port firt the Linux Ubuntu installation, assuming the programmer is connected before any other USB device get conneted (USART Debugger). 
AVRDUDE	= avrdude -p m168 -c stk500v2 -y -P /dev/ttyUSB1 # This port firt the Linux Ubuntu installation (this port number works when the USART debugger was connected first.
#DEBUG    = -Os -gdwarf-2
DEBUG		= -Os -g 
SIZE_TOOL_DETAILS=avr-nm
SIZE_TOOL_DETAILS_OPTIONS=--size-sort --radix=dec

# Define the include folders and additional pragmas.
CFLAGS = -DDEBUG_LEVEL=0 -DADVANCED_SWAY -D_I2C_ON -D_PROGMEM_DATA -DFIXMATH_FAST_SIN -DFIXMATH_NO_64BIT -DFIXMATH_NO_CACHE -DFIXMATH_OPTIMIZE_8BIT -DFIXEDPOINT_MATH -UFIXMATH_FULL_LIBRARY -UFLOATINGPOINT_MATH 

CFLAGS_RELEASE  =
#CFLAGS_DEBUG  =  
CFLAGS_DEBUG  = -DUSART_DEBUG -DUSART_DEBUG_SMALL_SIZE 
#CFLAGS_DEBUG  = -DDEBUG_LEVEL=0 -DUSART_DEBUG -DUSART_TRACIG -D_PROGMEM_DATA -D_TEST_MODE -D_I2C_OFF -D_SRF02_DEBUG

#Define all the subfolders for the project
#INCLUDE_PATH = -I. -IUSART -Ii2c -Isrf02 -Ieeprom
INCLUDE_PATH = -I. -Iir -IUSART -Ifixmath -L.
INCLUDE_LIBS = 
#INCLUDE_LIBS = -lm

######################################
# define the structure of the project. 
######################################
SOURCES = \
main.lizard.2.c \
USART/usart.c \
USART/tx.c \
USART/txstr.c \
USART/rx.c \
fixmath/fix16.c \
fixmath/fix16_sqrt.c \
fixmath/fix16_trig.c \

SOURCES_DEBUG   = 


#####################################
# Compilation instructions
#####################################
# define the set of compilation options.
#RELEASE:
COMPILE_RELEASE = avr-gcc -Wall $(DEBUG) -fpack-struct -fshort-enums -DF_CPU=$(F_CPU) $(INCLUDE_PATH) $(CFLAGS) $(CFLAGS_RELEASE) -mmcu=$(DEVICE) $(INCLUDE_LIBS)
#DEBUG
COMPILE_DEBUG = avr-gcc -Wall $(DEBUG) -fpack-struct -fshort-enums -DF_CPU=$(F_CPU) $(INCLUDE_PATH) $(CFLAGS) $(CFLAGS_DEBUG) -mmcu=$(DEVICE) $(INCLUDE_LIBS)


# symbolic targets:
help:
	@echo "quadpod brain - control logic for 12-servo quadpod"
	@echo "---------------------------------------------------------------------"
	@echo "This makefile builds the firmware for a quadpod."
	@echo "This version of the firmware uses static kinematics data, stored in"
	@echo "program memery. The USART link can be used to swap 3 different"
	@echo "directions of the movement vector. The length of step, height and"
	@echo "direction is given, cannot be altered".
	@echo " "
	@echo "See the quadpod.h for detailed description".
	@echo " "
	@echo "This Makefile has no default rule. Use one of the following:"
	@echo "make release .......... to build main.hex"
	@echo "make debug ............ to build main.hex"
	@echo "make program .......... to flash fuses and firmware for TX"
	@echo "make fuse_write ....... to flash the fuses"
	@echo "make fuse_read ........ to receive the fuses"
	@echo "make clean ............ to delete objects and hex file"
	@echo "make clear ............ to delete objects and hex file"
	@echo "make tags ............. to create TAG library" 
	@echo "                        (requires http://ctags.sourceforge.net)"

# Define the rules for TAG library generation. The ctags must be installed. http://ctags.sourceforge.net
tags: *.c *.h
	$(TAGS) main.lizard.2.c quadpod.h quadpod.fp.h ir/irdecoder.h USART/*.c USART/*.h fixmath/fix16.c  fixmath/fix16_sqrt.c  fixmath/fix16_trig.c 

all: debug
	@echo "DONE"

# Define the main project output.
release: $(SOURCES) Makefile
	@echo "*** Attention: Compiling the application with following falgs: ***"
	@echo $(CFLAGS)
	@echo $(CFLAGS_RELEASE)
	@echo "***"
	$(COMPILE_RELEASE) -o $(PROJECT).elf $(SOURCES)
	$(RM) $(PROJECT).hex $(PROJECT).eep.hex
	avr-objcopy -j .eeprom --set-section-flags .eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(PROJECT).elf $(PROJECT).eep.hex
	avr-objcopy -j .text -j .data -O ihex -R .eeprom $(PROJECT).elf $(PROJECT).hex
	avr-size --format=avr --mcu=$(DEVICE) --radix=10 $(PROJECT).elf
	@echo "** RELEASE FINISHED **"

debug: $(SOURCES) $(SOURCES_DEBUG) Makefile
	@echo "*** Attention: Compiling the application with following falgs: ***"
	@echo $(CFLAGS)
	@echo $(CFLAGS_DEBUG)
	@echo "***"
	$(COMPILE_DEBUG) -o $(PROJECT).elf $(SOURCES) $(SOURCES_DEBUG)
	$(RM) $(PROJECT).hex $(PROJECT).eep.hex
	avr-objcopy -j .eeprom --set-section-flags .eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(PROJECT).elf $(PROJECT).eep.hex
	avr-objcopy -j .text -j .data -O ihex -R .eeprom $(PROJECT).elf $(PROJECT).hex
	avr-size --format=avr --mcu=$(DEVICE) --radix=10 $(PROJECT).elf
	@echo "** DEBUG FINISHED **"

#debug: main.hex COMPILE := $(COMPILE_DEBUG) OBJECTS := $(OBJECTS_DEBUG)

#program: flash fuse
program: $(PROJECT).hex $(PROJECT).eep.hex
	$(AVRDUDE) -e -U flash:w:$(PROJECT).hex:i
	$(AVRDUDE) -D -U eeprom:w:$(PROJECT).eep.hex:i

size: $(PROJECT).elf
	$(SIZE_TOOL_DETAILS) $(SIZE_TOOL_DETAILS_OPTIONS) $(PROJECT).elf

# debugging targets:

disasm: $(PROJECT).elf
	avr-objdump -d $(PROJECT).elf

#
# duplicate the cleaning option ...
clear: clean


# Generic rule for assembling Assembler source files:
#.S.o:
#	$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

# Generic rule for compiling C to assembler, used for debugging only.
#.c.s:
#	$(COMPILE) -S $< -o $@


# rule for deleting dependent files (those which can be built by Make):
clean:
	$(RM) *.hex *.elf *.lst *.cof *.list *.map *.eep.hex *.s *.o USART/*.o


# rule for programming fuse bits:
fuse_write:
	@[ "$(FUSE_H)" != "" -a "$(FUSE_L)" != "" -a "$(FUSE_E)" != "" ] || \
		{ echo "*** Edit Makefile and choose values for FUSE_L, FUSE_H and FUSE_E!"; exit 1; }
#	$(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m
	$(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m -U efuse:w:$(FUSE_E):m 

fuse_read:
#	$(AVRDUDE) -U lfuse:r:-:h -U hfuse:r:-:h
	$(AVRDUDE) -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h 

##############################################################################
# Fuse values for particular devices
##############################################################################
# If your device is not listed here, go to
# http://palmavr.sourceforge.net/cgi-bin/fc.cgi
# and choose options for external crystal clock and no clock divider
#
################################## ATMega8 ##################################
# ATMega8 FUSE_L (Fuse low byte):
# 0x9f = 1 0 0 1   1 1 1 1
#        ^ ^ \ /   \--+--/
#        | |  |       +------- CKSEL 3..0 (external >8M crystal)
#        | |  +--------------- SUT 1..0 (crystal osc, BOD enabled)
#        | +------------------ BODEN (BrownOut Detector enabled)
#        +-------------------- BODLEVEL (2.7V)
# ATMega8 FUSE_H (Fuse high byte):
# 0xc9 = 1 1 0 0   1 0 0 1 <-- BOOTRST (boot reset vector at 0x0000)
#        ^ ^ ^ ^   ^ ^ ^------ BOOTSZ0
#        | | | |   | +-------- BOOTSZ1
#        | | | |   + --------- EESAVE (don't preserve EEPROM over chip erase)
#        | | | +-------------- CKOPT (full output swing)
#        | | +---------------- SPIEN (allow serial programming)
#        | +------------------ WDTON (WDT not always on)
#        +-------------------- RSTDISBL (reset pin is enabled)
#
############################## ATMega48/88/168 ##############################
# ATMega*8 FUSE_L (Fuse low byte):
# 0xdf = 1 1 0 1   1 1 1 1
#        ^ ^ \ /   \--+--/
#        | |  |       +------- CKSEL 3..0 (external >8M crystal)
#        | |  +--------------- SUT 1..0 (crystal osc, BOD enabled)
#        | +------------------ CKOUT (if 0: Clock output enabled)
#        +-------------------- CKDIV8 (if 0: divide by 8)
# ATMega*8 FUSE_H (Fuse high byte):
# 0xde = 1 1 0 1   1 1 1 0
#        ^ ^ ^ ^   ^ \-+-/
#        | | | |   |   +------ BODLEVEL 0..2 (110 = 1.8 V)
#        | | | |   + --------- EESAVE (preserve EEPROM over chip erase)
#        | | | +-------------- WDTON (if 0: watchdog always on)
#        | | +---------------- SPIEN (allow serial programming)
#        | +------------------ DWEN (debug wire enable)
#        +-------------------- RSTDISBL (reset pin is enabled)
#
############################## ATTiny25/45/85 ###############################
# ATMega*5 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0   1 1 1 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)
#        | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
#        +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATMega*5 FUSE_H (Fuse high byte):
# 0xdd = 1 1 0 1   1 1 0 1
#        ^ ^ ^ ^   ^ \-+-/ 
#        | | | |   |   +------ BODLEVEL 2..0 (brownout trigger level -> 2.7V)
#        | | | |   +---------- EESAVE (preserve EEPROM on Chip Erase -> not preserved)
#        | | | +-------------- WDTON (watchdog timer always on -> disable)
#        | | +---------------- SPIEN (enable serial programming -> enabled)
#        | +------------------ DWEN (debug wire enable)
#        +-------------------- RSTDISBL (disable external reset -> enabled)
#
################################ ATTiny2313 #################################
# ATTiny2313 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0   1 1 1 1
#        ^ ^ \+/   \--+--/
#        | |  |       +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
#        | |  +--------------- SUT 1..0 (BOD enabled, fast rising power)
#        | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
#        +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATTiny2313 FUSE_H (Fuse high byte):
# 0xdb = 1 1 0 1   1 0 1 1
#        ^ ^ ^ ^   \-+-/ ^
#        | | | |     |   +---- RSTDISBL (disable external reset -> enabled)
#        | | | |     +-------- BODLEVEL 2..0 (brownout trigger level -> 2.7V)
#        | | | +-------------- WDTON (watchdog timer always on -> disable)
#        | | +---------------- SPIEN (enable serial programming -> enabled)
#        | +------------------ EESAVE (preserve EEPROM on Chip Erase -> not preserved)
#        +-------------------- DWEN (debug wire enable)


